package study.rocketmq.usespringboot;

import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;
import study.rocketmq.vo.Order;

import javax.annotation.Resource;

@Service
public class SprintBootConsumerService {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Service
    @RocketMQMessageListener(consumerGroup = "my-consumer_tag", topic = "springboot-topic-tag")
    public static class TagConsumer implements RocketMQListener<String> {

        @Override
        public void onMessage(String message) {
            System.err.println("接收Tag到消息：" + message);
        }
    }
    @Service
    @RocketMQMessageListener(consumerGroup = "my-consumer_order", topic = "springboot-topic-order")
    public static class OrderConsumer1 implements RocketMQListener<Order> {

        @Override
        public void onMessage(Order order) {
            System.err.println("接收到order对象消息：" + order);
        }
    }
    @Service
    @RocketMQMessageListener(consumerGroup = "my-consumer_delay", topic = "springboot-topic-delay")
    public static class DelayConsumer implements RocketMQListener<String> {

        @Override
        public void onMessage(String message) {
            System.err.println("接收Delay到消息：" + message);
        }
    }
    /**
     * TODO  普通消费
     */
    @Service
//    @RocketMQMessageListener(consumerGroup = "my-consumer_No_orderly", topic = "springboot-topic-orderly")
    public static class OrderyConsumer_NoOrdery implements RocketMQListener<Order> {

        @Override
        public void onMessage(Order order) {
            if("5".equals(order.getOrderId())
                    || "batch_id_5".equals(order.getOrderId())){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.err.println(Thread.currentThread().getName()+"接收Oldery到消息：" + order);
        }
    }
    /**
     * TODO  顺序消息 :  consumeMode = ConsumeMode.ORDERLY
     */
    @Service
    @RocketMQMessageListener(consumerGroup = "my-consumer_orderly", topic = "springboot-topic-orderly"
           , consumeMode = ConsumeMode.ORDERLY)
    public static class OrderyConsumer implements RocketMQListener<Order> {

        @Override
        public void onMessage(Order order) {
            if("5".equals(order.getOrderId())
            || "batch_id_5".equals(order.getOrderId())){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.err.println(Thread.currentThread().getName()+"--------接收Oldery到消息：" + order);
        }
    }



}

